package cn.edu.zzuli.nothinglink.service.impl;

import cn.edu.zzuli.nothinglink.entity.MessageRecord;
import cn.edu.zzuli.nothinglink.entity.R;
import cn.edu.zzuli.nothinglink.mapper.CourseMapper;
import cn.edu.zzuli.nothinglink.mapper.MessageRecordMapper;
import cn.edu.zzuli.nothinglink.mapper.StudentsMapper;
import cn.edu.zzuli.nothinglink.service.MessageRecordService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author suzyyy
 * @since 2021-04-08
 */
@Service
public class MessageRecordServiceImpl extends ServiceImpl<MessageRecordMapper, MessageRecord> implements MessageRecordService {

    @Resource
    CourseMapper courseMapper;

    @Resource
    StudentsMapper studentsMapper;

    @Override
    public R addRecord(MessageRecord messageRecord) {
        //将json里的数据进行正确的转换
        JSONObject content = new JSONObject();
        content.put("content", messageRecord.getMesStuInfo());
        messageRecord.setMesStuInfo(content.toJSONString());

        save(messageRecord);
        return R.success();
    }

    @Override
    public R getMsgRecordBy(boolean isStu, Integer id) {
        List<MessageRecord> records;
        if (isStu) {
            //学生查询
            records = list(
                    new LambdaQueryWrapper<MessageRecord>().eq(MessageRecord::getMesStuId, id)
                            .orderByDesc(MessageRecord::getCreateTime)
            );
            ArrayList<Integer> courseIds = new ArrayList<>();

            records.stream().forEach(record -> {
                courseIds.add(record.getMesCourseId());
            });

            Map<Integer, String> courseMap = courseMapper.selectBatchIds(courseIds).stream()
                    .collect(Collectors.toMap(k -> k.getCourseId(), v -> v.getCourseName()));
            records.stream().forEach(record -> {
                record.setCourseName(courseMap.get(record.getMesCourseId()));
            });

        }else {
            //教师查询
            records = list(
                    new LambdaQueryWrapper<MessageRecord>().eq(MessageRecord::getMesTeaId, id)
                            .orderByDesc(MessageRecord::getCreateTime)
            );
            ArrayList<Integer> courseIds = new ArrayList<>();
            ArrayList<Integer> stuIds = new ArrayList<>();
            records.stream().forEach(record -> {
                courseIds.add(record.getMesCourseId());
                stuIds.add(record.getMesStuId());
            });
            Map<Integer, String> stusMap = studentsMapper.selectBatchIds(stuIds).stream()
                    .collect(Collectors.toMap(k -> k.getStuId(), v -> v.getStuName()));

            Map<Integer, String> courseMap = courseMapper.selectBatchIds(courseIds).stream()
                    .collect(Collectors.toMap(k -> k.getCourseId(), v -> v.getCourseName()));

            records.stream().forEach(record -> {
                record.setCourseName(courseMap.get(record.getMesCourseId()));
                record.setStuName(stusMap.get(record.getMesStuId()));
            });


        }
        return R.success().add("records", records);
    }

    @Override
    public R replyStu(JSONObject json) {
        JSONObject content = new JSONObject();
        content.put("content", json.getString("content"));
        MessageRecord messageRecord = new MessageRecord();
        messageRecord.setMesId(json.getInteger("msgId")).setMesTeaInfo(content.toJSONString());
        this.updateById(messageRecord);
        return R.success();
    }
}
